const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    notes: '',
    msg:'',
    disabled:false,
    book_id:'',
    book_info:[],
  },

  onLoad:function(options){
    let _this = this;
    _this.setData({
      book_id:options.book_id
    })
    _this.getBookInfo();
  },

  textareaAInput(e) {
    this.setData({
      notes: e.detail.value
    })
  },

  //获取账本信息
  getBookInfo:function(){
    let _this = this,
      book_id = _this.data.book_id;
    app._post('api/member/getBookInfo', {book_id:book_id}, function(result) {
      if(result.code == 200){
        _this.setData({
          book_info:result.data
        })
      }else{
        wx.showToast({
          title: _this.data.msg,
          icon: 'none',
          duration: 1500
        })
      }
    });
  },

  /*表单验证*/
  validation: function(values) {
    let _this = this;
    if (values.book_name == '') {
      _this.data.msg = '账本名称不能为空';
      return false;
    }
    return true;
  },

  /*表单提交*/
  formSubmit: function(e) {
    let _this = this,
      values = e.detail.value;

    // 表单验证
    if (!_this.validation(values)){
      wx.showToast({
        title: _this.data.msg,
        icon: 'none',
        duration: 1500
      })
      return false;
    }

    _this.setData({disabled:true}); //禁止重复点击提交按钮

    app._post('api/member/createUserBook',
      {
        book_name:values.book_name,
        notes:_this.data.notes,
      }, 
    function(result) {
      if(result.code == 200){
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000) 
      }
    }, false, function() {
      // 解除禁用
      _this.setData({
        disabled: false
      });
    });
  },
})